﻿#region Copyright information
// 
// Copyright © 2005-2013 Yongkee Cho. All rights reserved.
// 
// This code is a part of the Biological Object Library and governed under the terms of the
// GNU Lesser General  Public License (LGPL) version 2.1 which accompanies this distribution.
// For more information on the LGPL, please visit http://bol.codeplex.com/license.
// 
// - Filename: ConditionTreeNode.cs
// - Author: Yongkee Cho
// - Email: yongkeecho@gmail.com
// - Date Created: 2012-12-08 8:26 AM
// - Last Modified: 2013-01-25 3:59 PM
// 
#endregion
using System;
using System.Linq.Expressions;

namespace BOL.Algorithms.Classification
{
    public class ConditionTreeNode<TClass> : DecisionTreeNodeBase<TClass>
    {
        #region Public properties

        public Expression<Func<dynamic, dynamic>> Expression { get; set; }

        public Func<dynamic, dynamic> Condition { get; set; }

        public int FeatureIndex { get; set; }

        #endregion

        #region Constructor

        public ConditionTreeNode(dynamic key, int featureIndex, Expression<Func<dynamic, dynamic>> expression)
            : base((object)key)
        {
            if (expression == null)
                throw new ArgumentNullException("expression");

            FeatureIndex = featureIndex;
            Expression = expression;
            Condition = Expression.Compile();
        }

        public ConditionTreeNode(dynamic key)
            : base((object)key) { }

        public ConditionTreeNode()
            : base(null) { }

        #endregion

        #region Object overriden

        public override int GetHashCode()
        {
            var hashCode = base.GetHashCode() ^ FeatureIndex;
            if (Condition != null) hashCode ^= Condition.GetHashCode();

            return hashCode;
        }

        public override string ToString()
        {
            return Expression == null ? String.Format("{0}{1}", base.ToString(), FeatureIndex) :
                String.Format("{0}{1}{2}?", base.ToString(), Expression.Body, FeatureIndex);
        }

        #endregion
    }
}